package com.zty.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zty.inteceptor.SqlBeautyInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author ZTY
 * @Description 将拦截器加载进来
 * @Date 19:00 2023-12-11
 **/

@Configuration
public class MybatisConfiguration {

    @Bean
    @ConditionalOnProperty(name = "sql.beauty.show", // 指定配置属性的名称
            havingValue = "true", // 当配置属性等于"true"时条件成立
            matchIfMissing = true) // 如果没有配置这个属性，默认条件成立
    public SqlBeautyInterceptor sqlBeautyInterceptor(){
        return new SqlBeautyInterceptor();
    }


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        // MybatisPlusInterceptor是MyBatis-Plus的拦截器链
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();

        // 向拦截器链中添加一个分页拦截器PaginationInnerInterceptor
        // PaginationInnerInterceptor是MyBatis-Plus提供的分页拦截器，
        // 它可以拦截查询操作，自动实现分页功能
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());

        return mybatisPlusInterceptor;
    }

}
